package com.tesis_2.UI;

import java.util.ArrayList;

import org.json.JSONException;

import com.example.t2_clientback.R;

import com.tesis_2.BL.BLRestaurants;
import com.tesis_2.MO.MORestaurant;

import com.tesis_2.Utils.UTSeleccionar_Rest_Local_Adapter;

import android.os.Bundle;

import android.app.ListActivity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.ListView;

public class UISeleccionarRestLocal extends ListActivity{

	private Integer modo;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle b = getIntent().getExtras();
		modo = b.getInt("modo");
		ArrayList<MORestaurant> lsRest = new ArrayList<MORestaurant>();
		
		try {
			BLRestaurants.fillRestaurants();
			lsRest = BLRestaurants.getLsRest();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		setListAdapter( new UTSeleccionar_Rest_Local_Adapter(this, lsRest));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_uiseleccionar_rest_local,
				menu);
		return true;
	}
	
	public void onListItemClick(ListView l, View v, int position, long id) {
        // Do something when a list item is clicked
		if (modo == 1)
		{
			Intent intent = new Intent(getApplicationContext(), UIRegistrarLocal.class);
			MORestaurant rs = (MORestaurant) getListAdapter().getItem(position);
			intent.putExtra("RestId", rs.getId());
			startActivity(intent);
		}
		
		if (modo==2)
		{
			Intent intent = new Intent(getApplicationContext(), UIRegistrarPlato.class);
			MORestaurant rs = (MORestaurant) getListAdapter().getItem(position);
			intent.putExtra("RestId", rs.getId());
			startActivity(intent);
		}
		
		//;
    }

}
